<?php
class model_price extends framework_base_model {
	/**
	 * @desc 添加价格字段
	 * @param array 
	 * @return number
	 */
	public static function price_add($data) {
    	if(!is_array($data)) {
    		return false;
    	}//var_dump($data);exit;
    	try{
    		$sql = "insert into `weixin_product_price` (`parent_id`,`price_name`,`depth`,`product_class_id`,`is_show`,`select_all`,`sort`,`raw_add_time`) values(:parent_id,:price_name,:depth,:product_class_id,:is_show,:select_all,:sort,NOW())";
			$binds = array(':parent_id'=>0,':price_name'=>'',':depth'=>1,':product_class_id'=>$data['price_id'],':is_show'=>0,':select_all'=>0);
    		$pdo = self::DB() -> dbh;
			$pdo -> beginTransaction();
    		$stm = $pdo-> prepare($sql);
			//一级字段
			$binds[':price_name'] = trim($data['price_name1']);
			$binds[':is_show'] = $data['is_show'];
			$binds[':select_all'] = $data['select_all'];
			$binds[':sort'] = intval($data['sort1']);
			$stm -> execute($binds);
			$binds[':is_show'] = 0;$binds[':select_all'] = 0;
			$id = $pdo -> lastInsertId();
			//二级字段
			if(!empty($data['price_name2']))
				foreach($data['price_name2'] as $k => $v){
					$v = trim($v);
					if(!empty($v)){
						$binds[':price_name'] = $v;
						$binds[':parent_id'] = $id;
						$binds[':depth'] = 2;
						$binds[':sort'] = intval($data['sort2'][$k]);
						$stm -> execute($binds);
						$ids[$k] = $pdo -> lastInsertId();
					}
				}
			//三级字段
			if(!empty($data['price_name3']))
				foreach($data['price_name3'] as $key => $value){
					foreach($value as $v){
						$v = trim($v);
						if(!empty($v) && !empty($ids[$key])){
							$binds[':price_name'] = $v;
							$binds[':parent_id'] = $ids[$key];
							$binds[':depth'] = 3;
							$binds[':sort'] = intval($data['sort3'][$k]);
							$stm -> execute($binds);
						}
					}					
				}

			$pdo -> commit();
			return true;
    	}catch(Exception $e){
			$pdo -> rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
    }
    /*
     * 根据ID获取价格字段
     */
    public static function get_price_list_by_id(array $data, $id) {
		$return = array();
    	if(empty($id) || empty($data)){
			return $return;
		}
    	foreach($data as $k => $v){
			if($v['id'] == $id){
				$return = $v;
			}
		}
    	return $return;
    }
    /**
     * @desc 更新价格字段
     * @param unknown $id
     * @param unknown $data
     * @return unknown|number
     */
    public static function price_edit($id,$data) {
		if(empty($id) || empty($data)){
			return false;
		}
		$id = intval($id);
    	try{
    		$sql = "insert into `weixin_product_price` (`parent_id`,`price_name`,`depth`,`product_class_id`,`is_show`,`select_all`,`sort`,`raw_add_time`) values(:parent_id,:price_name,:depth,:product_class_id,:is_show,:select_all,:sort,NOW())";
			$sql1 = "update	`weixin_product_price` set `price_name` = :price_name,`is_show` = :is_show,`select_all` = :select_all,`sort`=:sort where `product_price_id` = :product_price_id";
			$sql2 = "select `product_price_id` from `weixin_product_price` where `parent_id` = {$id} and `delete_flag` = 0 order by `product_price_id` desc ";
			$sql3 = "select `product_price_id`,`parent_id` from `weixin_product_price` where `parent_id` in (select `product_price_id` from `weixin_product_price` where parent_id = {$id} and `delete_flag` = 0) and `delete_flag` = 0 order by `product_price_id` desc";
			$sql4 = "update	`weixin_product_price` set `delete_flag` = 1 where `parent_id` = :parent_id";
			$sql5 = "update	`weixin_product_price` set `delete_flag` = 1 where `product_price_id` = :product_price_id";
			$binds = array();
    		$pdo = self::DB() -> dbh;
			$pdo -> beginTransaction();
			$stm = $pdo-> prepare($sql);
    		$stm1 = $pdo-> prepare($sql1);
			//一级字段
			$binds[':price_name'] = trim($data['price_name1']);
			$binds[':is_show'] = $data['is_show'];
			$binds[':select_all'] = $data['select_all'];
			$binds[':sort'] = $data['sort1'];
			$binds[':product_price_id'] = $id;			
			$stm1 -> execute($binds);
			//二级字段
			$stm2 = $pdo-> prepare($sql2);
			$stm2->execute();
			$raw2 = $stm2->fetchAll();
			if($raw2){
				foreach($raw2 as $v){
					$raws[] = $v['product_price_id'];
				}
			}
			if(!empty($data['price_name2'])){
				foreach($data['price_name2'] as $k => $v){
					$v = trim($v);
					if(!empty($v) && !empty($raws[$k])){
						$binds = array();
						$binds[':price_name'] = $v;
						$binds[':is_show'] = 0;
						$binds[':select_all'] = 0;
						$binds[':sort'] = intval($data['sort2'][$k]);
						$binds[':product_price_id'] = $raws[$k];
						$stm1 -> execute($binds);
					}else if(!empty($v)){
						$binds = array();
						$binds[':price_name'] = $v;
						$binds[':parent_id'] = $id;
						$binds[':depth'] = 2;
						$binds[':is_show'] = 0;
						$binds[':select_all'] = 0;
						$binds[':sort'] = intval($data['sort2'][$k]);
						$binds[':product_class_id'] = $data['price_id'];
						$stm -> execute($binds);
						$raws[] = $pdo->lastInsertId();
					}else{
						$binds = array();
						$stm5 = $pdo-> prepare($sql5);
						$binds[':product_price_id'] = $raws[$k];
						$stm5 -> execute($binds);
					}
				}
			}
			//三级字段
			$stm3 = $pdo-> prepare($sql3);
			$stm3 -> execute();
			$raw2 = $stm3->fetchAll();
			if($raw2){
				foreach($raw2 as $v){
					$raw[$v['parent_id']][] = $v['product_price_id'];
				}
			}
			if(!empty($data['price_name3']))
				foreach($data['price_name3'] as $key => $value){
					foreach($value as $k => $v){
						$v = trim($v);
						if(!empty($v) && !empty($raw[$raws[$key]][$k])){
							$binds = array();
							$binds[':price_name'] = $v;
							$binds[':is_show'] = 0;
							$binds[':select_all'] = 0;
							$binds[':sort'] = intval($data['sort3'][$k]);
							$binds[':product_price_id'] = $raw[$raws[$key]][$k];
							$stm1 -> execute($binds);
						}else if(!empty($v)){
							$binds = array();
							$binds[':price_name'] = $v;
							$binds[':parent_id'] = $raws[$key];
							$binds[':depth'] = 3;
							$binds[':is_show'] = 0;
							$binds[':select_all'] = 0;
							$binds[':sort'] = intval($data['sort3'][$k]);
							$binds[':product_class_id'] = $data['price_id'];
							$stm -> execute($binds);
						}else{
							$binds = array();
							$stm5 = $pdo-> prepare($sql5);
							$binds[':product_price_id'] = $raw[$raws[$key]][$k];
							$stm5 -> execute($binds);
						}
					}					
				}

			$pdo -> commit();
			return true;
    	}catch(Exception $e){
			$pdo -> rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
    }
    /**
     * 获取价格字段列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function get_price_list($nextNum = 0,$pagesize = 0,$condition = array()) {
    	$sql = "select `product_price_id` as id,`parent_id` as parentid,`price_name`,`depth`,`is_show`,`select_all`,`sort` from `weixin_product_price`  where `delete_flag` = 0 ";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['price_name'])) {
    			$where .= " and `price_name` like '%{$condition['price_name']}%'";
    		}
			if(isset($condition['price_id'])) {
    			$where .= " and `product_class_id` = {$condition['price_id']}";
    		}
    	}
    	$where .= " order by `product_price_id` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		//$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		
		$sql = "select `parent_id`,`price_name`,`depth` from `weixin_product_price` where `delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	//删除价格字段
	public static function delete_price($id){
		if(empty($id)){
			return false;
		}
		$id = intval($id);
		try{
			//查询二级ID
			$sql1 = "select `product_price_id` as id from `weixin_product_price` where `parent_id` = :id";
			$pdo = self::DB() -> dbh;
			$pdo -> beginTransaction();

    		$stm1 = $pdo-> prepare($sql1);
			$stm1 -> execute(array(':id' => $id));
			$rows = $stm1 -> fetchAll();
			//查询一级ID
			if(!empty($rows)){
				foreach($rows as $v){
					$ids[] = $v['id'];
				}
				$sql3 = "select `product_price_id` as id from `weixin_product_price` where `parent_id` in (".implode(',',$ids).")";
				$stm3 = $pdo-> prepare($sql3);
				$stm3 -> execute();
				$rows = $stm3 -> fetchAll();
				if(!empty($rows)){
					foreach($rows as $v){
						$ids[] = $v['id'];
					}
				}
			}
			$ids[] = $id;
			//查询是否被使用
			$sqls = "select `price_attr_id` from `weixin_price_attr` where `attr_id` in (".implode(',',$ids).") and `delete_flag` = 0";
			$stms = $pdo->prepare($sqls);
			$stms -> execute();
			$result = $stms->fetchAll();
			if(!empty($result)){
				throw new Exception("price_attr_id is used");
			}
			//删除操作
			$sql2 = "update `weixin_product_price` set `delete_flag` = 1 where `product_price_id` in (".implode(',',$ids).")";
			$stm2 = $pdo-> prepare($sql2);
			$stm2 -> execute();
			$pdo -> commit();
			return true;
		}catch(Exception $e){
			$pdo -> rollBack();
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return false;
    	}
	}
}
